const mongoose = require('mongoose');

const releaseSchema = new mongoose.Schema({
  // 所属产品
  productId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Product',
    required: true,
    index: true
  },
  
  // 版本号 (语义化版本，如: 1.2.3)
  version: {
    type: String,
    required: true,
    index: true
  },
  
  // 环境: development | testing | production
  environment: {
    type: String,
    enum: ['development', 'testing', 'production'],
    required: true,
    index: true
  },
  
  // 包含的词条ID列表
  translationIds: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Translation'
  }],
  
  // 词条文件路径 (JSON文件在CDN上的路径)
  bundleUrls: {
    type: Map,
    of: String, // locale -> CDN URL
    default: new Map()
  },
  
  // 发布状态: pending | processing | success | failed
  status: {
    type: String,
    enum: ['pending', 'processing', 'success', 'failed'],
    default: 'pending',
    index: true
  },
  
  // 发布人
  releasedBy: {
    type: String,
    required: true
  },
  
  // 发布备注
  notes: {
    type: String,
    default: ''
  },
  
  // 错误信息 (如果发布失败)
  errorMessage: {
    type: String,
    default: null
  }
}, {
  timestamps: true
});

// 复合索引：产品 + 版本 + 环境
releaseSchema.index({ productId: 1, version: 1, environment: 1 }, { unique: true });

module.exports = mongoose.model('Release', releaseSchema);

